package com.dzh.spring.tools.excel;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JxlExpExcel {

	public static void main(String[] args) {
		String fileName = "D://jsl_test.xls";
		File file = new File(fileName);
		String[] titles = new String[] { "id", "name", "sex" };
		try {
			file.createNewFile();

			// 创建工作簿
			WritableWorkbook workbook = Workbook.createWorkbook(file);
			// 创建工作单元
			WritableSheet sheet = workbook.createSheet("sheet", 0);

			Label label = null;

			for (int i = 0; i < titles.length; i++) {

				label = new Label(i, 0, titles[i]);
				sheet.addCell(label);
			}

			workbook.write();
			workbook.close();
			
			readExcel(file);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void readExcel(File file){
		
		try {
			Workbook workbook = Workbook.getWorkbook(file);
			
			Sheet sheet = workbook.getSheet(0);
			
			for(int i =0 ;i<sheet.getRows();i++){
				
				for(int j = 0 ; j< sheet.getColumns();j++){
					
					Cell cell = sheet.getCell(j, i);
					System.out.print(cell.getContents()+"\t");
					
				}
				
			}
			workbook.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

}
